package gervosou;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class VueNouvCompte extends Vue {

	private ClientPhysique _client ;
	
	private JPanel contentPane;
	private JTextField textFieldNom;
	private JTextField textFieldPrenom;
	private JTextField textFieldNumero;
	private JTextField textFieldSolde;
	private JTextField textFieldAdresse;
	private JTextField textFieldNbComptes;
	private JTextField textFieldDecouvert;
	private JTextArea textAreaInfosComptes;
	
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JButton btnRechercher;
	private JButton btnAnnuler;
	private JButton btnEnregistrer;
	
	/**
	 * Create the frame.
	 */
	public VueNouvCompte(Controleur controleur) {
		super(controleur);
		setTitle("Enregistrement d'un nouveau compte pour un client existant");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 484, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblClient = new JLabel("Client :");
		lblClient.setBounds(6, 6, 61, 16);
		contentPane.add(lblClient);
		
		JLabel lblNom = new JLabel("Nom :");
		lblNom.setBounds(26, 34, 61, 16);
		contentPane.add(lblNom);
		
		JLabel lblPrenom = new JLabel("Pr�nom :");
		lblPrenom.setBounds(26, 61, 61, 16);
		contentPane.add(lblPrenom);
		
		textFieldNom = new JTextField();
		textFieldNom.setBounds(99, 28, 134, 28);
		contentPane.add(textFieldNom);
		textFieldNom.setColumns(10);
		
		textFieldPrenom = new JTextField();
		textFieldPrenom.setColumns(10);
		textFieldPrenom.setBounds(99, 55, 134, 28);
		contentPane.add(textFieldPrenom);
		
		JLabel lblAdresse = new JLabel("Adresse :");
		lblAdresse.setBounds(26, 95, 61, 16);
		contentPane.add(lblAdresse);
		
		textFieldAdresse = new JTextField();
		textFieldAdresse.setColumns(10);
		textFieldAdresse.setBounds(99, 89, 134, 28);
		contentPane.add(textFieldAdresse);
		
		JLabel lblNbComptes = new JLabel("Nb de comptes :");
		lblNbComptes.setBounds(237, 95, 104, 16);
		contentPane.add(lblNbComptes);
		
		textFieldNbComptes = new JTextField();
		textFieldNbComptes.setColumns(10);
		textFieldNbComptes.setBounds(344, 89, 134, 28);
		contentPane.add(textFieldNbComptes);
		
		
		btnRechercher = new JButton("Rechercher");
		btnRechercher.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String nom = textFieldNom.getText();
				String prenom = textFieldPrenom.getText();
				// liaison de la vue avec l'objet observe
				setClientPhysique (getControleur().rechClientPhysique(nom+prenom));
			}
			});
		
		btnRechercher.setBounds(278, 29, 117, 29);
		contentPane.add(btnRechercher);
		
		btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					getControleur().fermerVue(VueNouvCompte.this);}
		});
		btnAnnuler.setBounds(278, 56, 117, 29);
		contentPane.add(btnAnnuler);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(16, 123, 462, 47);
		contentPane.add(scrollPane);
		
		textAreaInfosComptes = new JTextArea();
		textAreaInfosComptes.setEditable(false);
		scrollPane.setColumnHeaderView(textAreaInfosComptes);

		JLabel lblCompte = new JLabel("Compte :");
		lblCompte.setBounds(6, 175, 61, 16);
		contentPane.add(lblCompte);
		
		JLabel lblNumero = new JLabel("Num\u00E9ro g\u00E9n\u00E9r\u00E9 :");
		lblNumero.setBounds(260, 244, 104, 16);
		contentPane.add(lblNumero);
		
		textFieldNumero = new JTextField();
		textFieldNumero.setColumns(10);
		textFieldNumero.setBounds(371, 238, 107, 28);
		contentPane.add(textFieldNumero);
		
		JLabel lblType = new JLabel("Type :");
		lblType.setBounds(26, 191, 61, 16);
		contentPane.add(lblType);
		
		final JRadioButton rdbtnCourant = new JRadioButton("Courant");
		buttonGroup.add(rdbtnCourant);
		rdbtnCourant.setBounds(89, 187, 104, 23);
		contentPane.add(rdbtnCourant);
		
		final JRadioButton rdbtnCEL = new JRadioButton("CEL");
		buttonGroup.add(rdbtnCEL);
		rdbtnCEL.setBounds(195, 187, 79, 23);
		contentPane.add(rdbtnCEL);
		
		final JRadioButton rdbtnPEL = new JRadioButton("PEL");
		buttonGroup.add(rdbtnPEL);
		rdbtnPEL.setBounds(278, 187, 141, 23);
		contentPane.add(rdbtnPEL);
		
		JLabel lblSoldeInitial = new JLabel("Solde initial :");
		lblSoldeInitial.setBounds(6, 222, 104, 16);
		contentPane.add(lblSoldeInitial);
		
		textFieldSolde = new JTextField();
		textFieldSolde.setColumns(10);
		textFieldSolde.setBounds(117, 216, 134, 28);
		contentPane.add(textFieldSolde);
		
		btnEnregistrer = new JButton("Enregistrer");
		btnEnregistrer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String s = textFieldSolde.getText();
				String type;
				if (rdbtnCourant.isSelected()) {
					type = "Courant";}
				else if (rdbtnCEL.isSelected()) {
					type = "CEL";}
				else {
					type = "PEL";}
				String d = textFieldDecouvert.getText();
				getControleur().nouvComptePerso(type, s, d, _client);
				}
		});
		btnEnregistrer.setBounds(361, 203, 117, 29);
		contentPane.add(btnEnregistrer);
		
		JLabel lblDecouvert = new JLabel("D\u00E9couvert autoris\u00E9 :");
		lblDecouvert.setBounds(6, 250, 141, 16);
		contentPane.add(lblDecouvert);
		
		textFieldDecouvert = new JTextField();
		textFieldDecouvert.setColumns(10);
		textFieldDecouvert.setBounds(156, 244, 95, 28);
		contentPane.add(textFieldDecouvert);
	}

	/**
	 * definition des etats de la fenetre
	 */
	public void setEtat (int etat){
		switch (etat) {
		case initiale: {
			textFieldAdresse.setEnabled(false);
			textFieldNbComptes.setEnabled(false);
			textFieldSolde.setEnabled(false);
			textFieldDecouvert.setEnabled(false);
			textFieldNumero.setEnabled(false);
			btnEnregistrer.setVisible(false);
			break;
			}
		case finale: {
			btnRechercher.setEnabled(false);
			textFieldAdresse.setEditable(false);
			textFieldNbComptes.setEditable(false);
			textFieldDecouvert.setEnabled(true);
			textFieldSolde.setEnabled(true);
			textFieldNumero.setEditable(false);
			btnEnregistrer.setVisible(true);
			btnEnregistrer.setEnabled(true);
			break;
			}
		}
	}
	
	private ClientPhysique getClientPhysique() {
		return _client;
	}
	
	private void setClientPhysique(ClientPhysique client) {
		 _client = client;
	}
	
	public void alimente(ClientPhysique clp) {
		textFieldAdresse.setText(clp.getAdresse());
		textFieldNbComptes.setText(String.valueOf(clp.getNbComptes()));
		textAreaInfosComptes.setText("");
		for (ComptePerso cp : clp.getComptes()) {
			  textAreaInfosComptes.append( "num�ro " + String.valueOf(cp.getNumero()) + " solde " + String.valueOf(cp.getSolde()) + " type " + cp.getType() + "\n");
		}
	}
	
	public void alimenteNum(int n) {
		textFieldNumero.setText(String.valueOf(n));
	}
}